<?php
    header("Content-type:text/html;charset=utf-8");
    if ($_POST) {
        include '../../api/config.php';
        include './comm.php';
        $json = $_POST["data"];
        $data = json_decode($json);
        $id = $data->head;
        if ($id == '1026') {
            $title = $data->title;
            $cont = $data->html;
            $cont1 = $data->md;
            $tag = $data->tag;
            $ids = $data->id;
            $book_img = $data->up_img;
            $sql5 = "UPDATE book SET title='{$title}',cont='{$cont}',cont1='{$cont1}',tag='{$tag}',book_img='{$book_img}' WHERE id='{$ids}'";
            //保存;

            if (flag($data)) {
                $stmt5 = $conn->prepare($sql5);//插入用prepare，避免提交一次插入两次的错误
                $stmt5->execute();
                $result5 = $stmt5->setFetchMode(PDO::FETCH_ASSOC);
                echo get_msg('200','操作成功~',false,false);
            } else {
                echo get_msg('500','权限不足~','../index.html',false);
            }
        }
        if ($id == '1024') {
            $title = $data->title;
            $cont = $data->html;
            $cont1 = $data->md;
            $tag = $data->tag;
            $book_img = $data->up_img;
            $sql2 = "INSERT INTO book(title,cont,cont1,tag,book_img) VALUES('{$title}','{$cont}','{$cont1}','{$tag}','{$book_img}')";
            //新建;
            if (flag($data)) {
                $stmt2 = $conn->prepare($sql2);
                $stmt2->execute();
                $result2 = $stmt2->setFetchMode(PDO::FETCH_ASSOC);
                echo get_msg('200','操作成功~',false,false);
            } else {
                echo get_msg('500','权限不足~','../index.html',false);
            }
        }

        if ($id == '1023') {
            $sql1 = "SELECT * FROM tag WHERE flag='1'";
            if (flag($data)) {
                $arr1 = array();
                $arr1['data'] = array();
                $stmt1 = $conn->query($sql1);
                $stmt1->execute();
                $result1 = $stmt1->setFetchMode(PDO::FETCH_ASSOC);
                $arr1['data'] = $stmt1->fetchAll();

                if ($result1) {
                    echo get_msg('200','操作成功~',false,$arr1['data']);
                } else {
                    echo get_msg('400','数据异常~',false,false);
                }
            } else {
                echo get_msg('500','权限不足~','../index.html',false);
            }
        }
        $conn = null;
    } else {
        echo "<script>\r\n    window.location.href = '../../';\r\n    </script>";
    }